<template>
	<view class="wp">
		<view class="fill_bg">
			<image class="bg" src="https://img.jinghushi.com/images/9/2024/08/ur3N5D9MO233OMN7V9z92oR0SVom73.png" mode="widthFix"></image>
			<view class="goOpen" @click="getInfo">确认支付</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				vipGoodsList: [],
				mid: -1,
				info: [],
				rate: '',
				infos: [],
				isGroupBuy:1,
			}
		},
		async onLoad(options) {
			if(options.mid){
				this.mid = options.mid;
			}
			this.rate = uni.getStorageSync('JPinTuan') == '-2' ? 1 : 0;
			this.getVipGoodsList();
			// await this.$onLaunched;
			if(!uni.getStorageSync('token')){
				this.getToken();
			}
		},
		methods: {
			getToken(){
				// #ifdef MP-WEIXIN
				uni.login({
					provider: 'weixin',
					success:res=> {
						this.$axios('login/autologin','POST','member',{
							mid: this.mid,
							code: res.code
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								this.getSharePath();
							}
						})
					}
				})
				// #endif
				// #ifdef MP-ALIPAY
				my.getAuthCode({
				    scopes: 'auth_base',
				    success: res => {
						this.$axios('login/autologinAliPay','POST','member',{
							mid: this.mid,
							code: res.authCode
						}).then(res=>{
							if(res.data.code == 200){
								uni.setStorageSync('token', res.data.data);
								uni.setStorageSync('Jh_recotabshops', 1);
								this.getSharePath();
							}
						})
					}
				})
				// #endif
			},
			getInfo(){
				// 获取商品信息
				uni.showLoading({
					title:'支付中',
					mask: true
				})
				this.$axios('confirm/display','POST','order',{
					id: this.vipGoodsList[0].id,
					optionid: 0,
					total: 1,
					freegoods: 0,
					rate: 0,
				}).then(res=>{
					if(res.data.code == 200){
						this.info = res.data.data;
						this.toBuyReal();
					}
				})
			},
			toBuyReal(){
				// 创建订单
				var that = this;
				var order = [];
				var Pto = '';
				var Pgoodsid = that.info.order_all[0].goods[0].goodsid;
				if (uni.getStorageSync('JPinTuan') && this.rate != '-2') {
					var JPinTuan = uni.getStorageSync('JPinTuan').split('||');
					if (JPinTuan[0] == Pgoodsid && JPinTuan[1] != '-1') {
						Pto = JPinTuan[1]
					}
				}
				for (var i = 0; i < that.info.order_all.length; i++) {
					var orders = {
						goods: []
					}
					orders.supplier_uid = that.info.order_all[i].supplier_uid
					for (var j = 0; j < that.info.order_all[i].goods.length; j++) {
						if (that.freegoods == 1) {
							that.info.order_all[i].goods[j].total = 1;
						}
						var goodstype = that.info.order_all[i].goods[j].goodsid + ',' + that.info.order_all[i].goods[j]
							.optionid + ',' +
							that.info.order_all[i].goods[j].total
						orders.goods.push(goodstype)
					}
					orders.goods = orders.goods.join('|')
					orders.id = that.id
					orders.dispatchtype = 0
					orders.fromcart = that.info.fromcart
					orders.cartids = 0
					orders.remark = that.info.order_all[i].remake
					orders.deduct = 0
					orders.deduct2 = 0
					orders.yunbi = 0;
					orders.freegoods = that.freegoods;
					orders.addressid = that.info.address ? that.info.address.id : '';
					orders.groupsn = Pto;
					if (that.info.order_all[i].couponinfo != '' && that.info.order_all[i].couponinfo) {
						orders.couponid = that.info.order_all[i].couponinfo.id
					}
					order.push(orders)
				}
				order = JSON.stringify(order)
				this.$axios('confirm/create', "POST", 'order', {
					order,
					rate: this.rate,
					type: this.typeStatus,
				}).then(res => {
					if (res.data.code == 200) {
						if (res.data.data.orderid == '') {
							uni.showToast({
								title: '请刷新重试',
								icon: 'none'
							})
						} else {
							this.payGoods(res.data.data.orderid);
						}
					}
				})
			},
			payGoods(orderid){
				// 支付
				let status=1;
				this.$axios('Pay/main', "POST", 'order', {
					orderid: orderid,
					apptype: 3,
					code: '',
					iswx: 1
				}).then(ups => {
					if (ups.data.code == 200) {
						this.infos = ups.data.data
						this.$axios('Pay/goPay', "GET", 'order', {
							type: status,
							ordersn: this.infos.order.ordersn,
							apptype: 3
						}).then(res => {
							uni.hideLoading();
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.data.timeStamp,
								nonceStr: res.data.data.nonceStr,
								package: res.data.data.package,
								signType: 'MD5',
								paySign: res.data.data.sign,
								success: res => {
									if (this.infos.order.issend == '0') {
										uni.setStorageSync('upOrderType',1);
									}
									if (this.infos.order.issend == '1') {
										uni.setStorageSync('upOrderType',2);
									}
									if(this.isGroupBuy==1){
										this.$navTo('/pages/money/groupBuySuccess?id='+orderid);//跳转到团购
									}else if(this.isGroupBuy==2){
										this.$navTo('/pages/user/card');//跳转到次卡
									}else{
										this.$navTo('/pages/index/order');
									}
								},
								fail: () => {
									uni.hideLoading();
									this.$api.msg('支付失败');
								}
							});
						})
					}
				})
			},
			getVipGoodsList(){
				this.$axios('home/vipgoodslist','POST','shop').then(res=>{
					if(res.data.code == 200){
						this.vipGoodsList = res.data.data.list;
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	page{
		background: #1c1817;
	}
	.wp {
		background: #1c1817;
		padding-bottom: 40rpx;
	}
	.fill_bg{
		position: relative;
		.bg{
			width: 100%;
		}
	}
	.goOpen{
		margin: 0 auto;
		width: 504rpx;
		line-height: 94rpx;
		background: linear-gradient( 90deg, #F6E6C7 0%, #EED192 100%);
		border-radius: 47rpx;
		font-size: 32rpx;
		font-weight: 600;
		color: #412116;
		text-align: center;
		// position: absolute;
	 //    bottom: 70rpx;
	 //    left: 0;
	 //    right: 0;	
	}
.xieyi{
	color: #999;
	font-size: 24rpx;
	padding: 20rpx;
	text{
		color: #d5b28f;
	}
}
</style>